Thread: Displaying incorrect values_what`s wrong?

  1. #1
    Registered User
    Join Date
    Mar 2017
    Posts
    52

    Displaying incorrect values_what`s wrong?

    Hi,

    I am trying bit flipping function. But, While executing its showing incorrect value. I don`t where code going wrong. Please help me anyone.

    Correct Codeword :[10010101]// First 4 bits are message bits.

    If i change any one of message bits, my code should detect and correct the correct codeword

    For exampl
    1. Error codeword ;[1,1,0,1,0,1,0,1], i should get result : [10010101]
    2. [0,0,0,1,0,1,0,1], i should get result : [10010101]

    Code:
    #include <stdio.h>
    int main()
    {
        int n,r,c,no_e,S,n0,n1;
        int y[10] = {0};
    
        int i,j,pos,I;
        int Imax = 10;
        int E[10][10] = {0};
    
    
    int H[4][8] = {{0,1,0,1,1,0,0,1},{1,1,1,0,0,1,0,0},{0,0,1,0,0,1,1,1},{1,0,0,1,1,0,1,0}};
    int M[8] = {1,1,0,1,0,1,0,1};// Error codeword
    int t[8];
    n = 8;
    r = 4;
    c = 8;
    
    //Decoding using Message passing in Erasure channel
        no_e = 0;
        S = 0;
        pos = 0;
        I = 0;
        while(I<Imax)
        {
            no_e = 0;
            for(j=0;j<r;j++)
                {
                    S = 0;
                    for(i=0;i<n;i++)
                    {
                        E[j][i] = -1;
                        S = S + H[i][j]*M[i];
                    }
                    for(i=0;i<n;i++)
                        if(H[j][i] == 1)
                            E[j][i] = (S+M[i])%2;
                }           //Finishing an entire matrix traverse
            printf("\n\n\n");
    
            for(j=0;j<n;j++)
            {
                n1 = 0;
                n0 = 0;
                for(i=0;i<r;i++)
                {//bcz E[i][j] can be -1 also
                    if((E[i][j]==0 || E[i][j]==1))
                        (E[i][j]==0)?n0++:n1++;
                }
                if(n0==n1)
                    (M[j]==0)?n0++:n1++;
                t[j] = (n0>=n1)?0:1;
                if(t[j]!=M[j])
                    M[j] = (M[j]+1)%2;  //bit flipping
            }
            for(j=0;j<r;j++)
            {
                S = 0;
                for(i=0;i<n;i++)
                    S = S + H[j][i]*M[i];
                if(S%2!=0)
                {
                    no_e++;
                    break;
                }
            }
            if(no_e==0)
                break;
            I = I+1;
        }
    
        printf("\n\n\nThe corrected Code Word is(%d): \n\n",I);
        for(i=0;i<n;i++)
            printf("%d\t",M[i]);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,664
    Unless you also post a description of your algorithm (a wikipedia page for example), we're in no position to even guess how you screwed up.

    Some decent formatting wouldn't go amiss in the meantime.

    Not to mention pruning out dead or useless code.
    Code:
    $ gcc -Wall -Wextra foo.c
    foo.c: In function ‘main’:
    foo.c:18:13: warning: variable ‘pos’ set but not used [-Wunused-but-set-variable]
       int i, j, pos, I;
                 ^
    foo.c:16:7: warning: unused variable ‘y’ [-Wunused-variable]
       int y[10] = { 0 };
           ^
    foo.c:15:13: warning: variable ‘c’ set but not used [-Wunused-but-set-variable]
       int n, r, c, no_e, S, n0, n1;
                 ^
    As for the problem, add some debug code to examine the state as you go (or just use a debugger).
    You should be able to work through the problem on paper in step with the code. When you find paper answer != code answer, you've found a bug!
    Whether that bug is in the code or on the paper is for you to figure out.

    Anyway,
    Code:
    #include <stdio.h>
    
    void dumpMat1(int pass, int E[10][10]) {
      printf("DEBUG PASS=%d\n", pass);
      for ( int r = 0 ; r < 10 ; r++ ) {
        for ( int c = 0 ; c < 10 ; c++ ) {
          printf("%d ", E[r][c]);
        }
        printf("\n");
      }
    }
    
    int main()
    {
      int n, r, c, no_e, S, n0, n1;
      int y[10] = { 0 };
    
      int i, j, pos, I;
      int Imax = 10;
      int E[10][10] = { 0 };
    
    
      int H[4][8] = {
        {0, 1, 0, 1, 1, 0, 0, 1},
        {1, 1, 1, 0, 0, 1, 0, 0},
        {0, 0, 1, 0, 0, 1, 1, 1},
        {1, 0, 0, 1, 1, 0, 1, 0}
      };
      int M[8] = { 1, 1, 0, 1, 0, 1, 0, 1 };  // Error codeword
      int t[8];
      n = 8;
      r = 4;
      c = 8;
    
    //Decoding using Message passing in Erasure channel
      no_e = 0;
      S = 0;
      pos = 0;
      I = 0;
      while (I < Imax) {
        no_e = 0;
        for (j = 0; j < r; j++) {
          S = 0;
          for (i = 0; i < n; i++) {
            E[j][i] = -1;
            S = S + H[i][j] * M[i];
          }
          for (i = 0; i < n; i++)
            if (H[j][i] == 1)
              E[j][i] = (S + M[i]) % 2;
        }                           //Finishing an entire matrix traverse
        ///printf("\n\n\n");
        dumpMat1(I,E);    // something useful, instead of masses of blank lines
    
        for (j = 0; j < n; j++) {
          n1 = 0;
          n0 = 0;
          for (i = 0; i < r; i++) { //bcz E[i][j] can be -1 also
            if ((E[i][j] == 0 || E[i][j] == 1))
              (E[i][j] == 0) ? n0++ : n1++;
          }
          if (n0 == n1)
            (M[j] == 0) ? n0++ : n1++;
          t[j] = (n0 >= n1) ? 0 : 1;
          if (t[j] != M[j])
            M[j] = (M[j] + 1) % 2;  //bit flipping
        }
        for (j = 0; j < r; j++) {
          S = 0;
          for (i = 0; i < n; i++)
            S = S + H[j][i] * M[i];
          if (S % 2 != 0) {
            no_e++;
            break;
          }
        }
        if (no_e == 0)
          break;
        I = I + 1;
      }
    
      printf("\n\n\nThe corrected Code Word is(%d): \n\n", I);
      for (i = 0; i < n; i++)
        printf("%d\t", M[i]);
      printf("\n");
    }
    Your first task is to figure out if each iteration of your E matrix is correct.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2017
    Posts
    52
    Sorry for Improper formatting. Thanks for your response

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program displaying wrong results
    By bkoper16 in forum C++ Programming
    Replies: 8
    Last Post: 03-03-2011, 10:13 AM
  2. Value is incorrect??
    By Myca in forum C Programming
    Replies: 3
    Last Post: 02-11-2011, 10:20 AM
  3. Incorrect Valules displaying in a struct member
    By kristentx in forum C++ Programming
    Replies: 7
    Last Post: 09-25-2007, 12:18 AM
  4. Is my algorithim incorrect?
    By leeor_net in forum C Programming
    Replies: 2
    Last Post: 04-04-2004, 04:14 PM
  5. Incorrect Input
    By CeeCee in forum C Programming
    Replies: 1
    Last Post: 11-26-2001, 12:25 PM

Tags for this Thread